package com.edu.ujn.myh.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.edu.ujn.myh.dao.ILoginDao;
import com.edu.ujn.myh.domain.PrivInfo;
import com.edu.ujn.myh.domain.RoleInfo;
import com.edu.ujn.myh.domain.RolePrivInfo;
import com.edu.ujn.myh.domain.TreeNode;
import com.edu.ujn.myh.domain.UserInfo;
import com.edu.ujn.myh.domain.UserRoleInfo;
import com.edu.ujn.myh.utils.HQL;

/**
 * 
 * @作者 马永华
 * @日期 2010-4-13下午09:17:53
 * @版本 1.0.0
 */
public class LoginDaoImpl extends HibernateDaoSupport implements ILoginDao {

	// 用户注册
	public void saveUser(UserInfo userInfo) {

		UserRoleInfo urInfo = new UserRoleInfo();
		RoleInfo roleInfo = new RoleInfo();
		roleInfo.setId(userInfo.getRoleId());
		this.getHibernateTemplate().save(userInfo);
		urInfo.setUserInfo(userInfo);
		urInfo.setRoleInfo(roleInfo);
		this.getHibernateTemplate().save(urInfo);

	}

	// 用户登录，成功返回用户的角色
	public List login(UserInfo userInfo) {
		List roleList = null;
		String hql = "from UserInfo userInfo where userInfo.userName = '"
				+ userInfo.getUserName() + "' and userInfo.passWord = '"
				+ userInfo.getPassWord() + "'";
		List list = this.getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			final String sql = "select role.r_id from t_role role ,t_user userInfo,t_userrole ur where ur.ur_roleid = role.r_id and ur.ur_userid = userInfo.u_id and userInfo.u_userName = '"
					+ userInfo.getUserName() + "'";
			roleList = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Query query = session.createSQLQuery(sql);
							return query.list();
						}
					});
		}
		return roleList;
	}
	public List userPrivList(final List roleInfoList) {
		List privList = null;
		if (roleInfoList != null && roleInfoList.size() > 0) {
			privList = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							TreeNode root = null;
							TreeNode node = null;
							List reList = new ArrayList();
							List childList = null;
							String sql = "select priv.p_id,priv.p_privname from "
									+ "t_role role ,t_rolepriv rp ,t_priv priv where role.r_id = rp.rp_roleid and "
									+ " rp.rp_privid = priv.p_id and priv.p_pid = 0";
							sql = HQL.sql(sql, roleInfoList, "role.r_id");
							Query query = session.createSQLQuery(sql);
							List temp = query.list();
							if (temp != null && temp.size() > 0) {
								for (int i = 0; i < temp.size(); i++) {
									root = new TreeNode();
									childList = new ArrayList();
									reList.add(root);
									Object[] obj = (Object[]) temp.get(i);
									int id = (Integer) obj[0];
									root.setNodeName((String) obj[1]);
									root.setChildren(childList);
									root.setLeaf(false);

									String temSql = "select priv.p_privname,priv.p_url from "
											+ "T_Role role ,T_RolePriv rp ,T_Priv priv where role.r_Id = rp.rp_RoleId and "
											+ " rp.rp_PrivId = priv.p_Id and priv.p_Pid = "
											+ id;
									temSql = HQL.sql(temSql, roleInfoList,
											"role.r_Id");
									Query query1 = session
											.createSQLQuery(temSql);
									List list1 = query1.list();
									if (list1 != null && list1.size() > 0) {
										for (int j = 0; j < list1.size(); j++) {
											node = new TreeNode();
											Object[] obj1 = (Object[]) list1
													.get(j);
											node.setNodeName((String) obj1[0]);
											node.setNodeUrl((String) obj1[1]);
											node.setLeaf(true);
											childList.add(node);
										}
									}
								}
							}
							return reList;
						}
					});
		}
		return privList;
	}

//检查用户名和密码
	public List checkUserName(String sql) {
		
		return this.getHibernateTemplate().find(sql);
	}
	
//获得用户角色列表
	public List roleList(String sql) {
		return this.getHibernateTemplate().find(sql);
	}

//	更新密码
	public void updatePassWord(String sql) {
		this.getHibernateTemplate().bulkUpdate(sql);
	}

public List rolePrivList(final RoleInfo roleInfo) {
	return this.getHibernateTemplate().executeFind(new HibernateCallback() {
		public Object doInHibernate(Session session)
				throws HibernateException, SQLException {
			String sql = "select priv.P_Id,priv.P_PrivName from T_Priv priv where priv.P_Pid = "+0;
			List retList = new ArrayList();
			TreeNode root = null;
			TreeNode node = null;
			List childrenList = null;
			Query query = session.createSQLQuery(sql);
			List list1 = query.list();
			if (list1 != null && list1.size() > 0) {
				for (int i = 0; i < list1.size(); i++) {
					Object[] obj = (Object[])list1.get(i);
					root = new TreeNode();
					childrenList = new ArrayList();
					int id = (Integer)obj[0];
					root.setNodeName((String)obj[1]);
					root.setNodeId(new Integer(id).toString());
					root.setChildren(childrenList);
					retList.add(root);
					String sql2 = "select * from T_RolePriv rp where rp.RP_PrivId = "+id+" and rp.RP_RoleId = "+roleInfo.getId();
						Query query2 = session.createSQLQuery(sql2);
						if(query2.list().size()>0){
							root.setChecked(true);
						}

					String sql1 = "select priv.P_Id,priv.P_PrivName,priv.P_Url from T_Priv priv where priv.P_Pid = "+id;
					Query query1 = session.createSQLQuery(sql1);
					List list2 = query1.list();
					if(list2!=null&&list2.size()>0){
						for(int j=0;j<list2.size();j++){
							Object[] obj1 = (Object[])list2.get(j);
							node = new TreeNode();
							childrenList.add(node);
							int priId = (Integer)obj1[0];
							node.setLeaf(true);
							node.setNodeId(new Integer(priId).toString());
							node.setNodeName((String)obj1[1]);
							node.setNodeUrl((String)obj1[2]);
							String sql3 = "select * from T_RolePriv rp where rp.RP_PrivId = "+priId+" and rp.RP_RoleId = "+roleInfo.getId();
							Query query3 =session.createSQLQuery(sql3);
							List list3 =query3.list();
							if(list3!=null){
								node.setChecked(list3.size()>0?true : false);
							}
						}
					}
				}
			}
			return retList;
		}
	});
}

public void updateRoleModify(int[] priv, int roleId) {
	String hql = "from RolePrivInfo rp where rp.roleInfo.id = "+roleId;//����ý�ɫ��ǰӵ�е�Ȩ��
	HibernateTemplate ht = this.getHibernateTemplate();
	List list = ht.find(hql);
	List ret = isContain(list,priv);
	List del = (List)ret.get(0);
	List add = (List)ret.get(1);
	for(int i=0;i<del.size();i++){
		RolePrivInfo rp = (RolePrivInfo)del.get(i);
		ht.delete(rp);
	}
	for(int j=0;j<add.size();j++){
		RolePrivInfo rpInfo = new RolePrivInfo();
		PrivInfo privInfo = new PrivInfo();
		privInfo.setId((Integer)add.get(j));
		RoleInfo roleInfo = new RoleInfo();
		roleInfo.setId(roleId);
		rpInfo.setPrivInfo(privInfo);
		rpInfo.setRoleInfo(roleInfo);
		ht.save(rpInfo);
	}
}

public List isContain(List list,int [] priv){
	List addList = new ArrayList();
	List ret = new ArrayList();
	ret.add(list);
	ret.add(addList);
	for(int j=0;j<priv.length;j++){
		int priId = priv[j];
		boolean flag = false;
		for(int i=0;i<list.size();i++){
			RolePrivInfo rp = (RolePrivInfo)list.get(i);
			int id = rp.getPrivInfo().getId();
			if(id==priId){
				flag = true;
				list.remove(rp);
				break;
			}
		}
		if(!flag){
			addList.add(priId);
		}
	}
	return ret;
}

public void updateRePass(UserInfo userInfo) {
		String sql = "update UserInfo u set u.passWord = '"+userInfo.getPassWord()+"' where u.userName = '"+userInfo.getUserName()+"' and u.mail = '"+userInfo.getMail()+"'";
		this.getHibernateTemplate().bulkUpdate(sql);
	}
}
